﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class Stock_FloorManager : System.Web.UI.Page
{
    private string action
    {
        get
        {
            if (ViewState["action"] == null)
                ViewState["action"] = "";    
            return ViewState["action"].ToString();
        }
        set { ViewState["action"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }

    }
    protected void init()
    {
        action = Request.QueryString["action"];
        DataBandStockList();
        DataBandAreaList(this.ddlStock.SelectedValue);

        DataBandSearchStockList();
        DataBandSearchAreaList(this.ddlStockSearch.SelectedValue);

        if (action != null)
        {
            if (action.ToString().Equals("edit"))
            {
                string ID = Request.QueryString["ID"].ToString();
                FloorBLL floorBLL = new FloorBLL();
                T_FLOOR floor = floorBLL.GetFloorByID(ID);

                this.ddlStock.SelectedIndex = this.ddlStock.Items.IndexOf(this.ddlStock.Items.FindByValue(floor.T_STOCK_AREA.STOCK_ID.ToString()));
                this.ddlArea.SelectedIndex = this.ddlArea.Items.IndexOf(this.ddlArea.Items.FindByValue(floor.AREA_ID.ToString()));
                this.txtFloorName.Text = floor.FloorName;

                updateButton.Text = "修改";

            }
            else if (action.ToString().Equals("del"))
            {
                string ID = Request.QueryString["ID"].ToString();
                FloorBLL floorBLL = new FloorBLL();
                floorBLL.DeleteFloor(ID);
            }
        }
        else
        {

            updateButton.Text = "添加";
        }

        DataBand();

    }
    /// <summary>
    /// 
    /// </summary>

    protected void DataBandStockList()
    {
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        this.ddlStock.DataSource = storeHouseBLL.GetStockList();
        this.ddlStock.DataBind();
    }

    protected void DataBandAreaList(string StockID)
    {
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        this.ddlArea.DataSource = storeHouseBLL.GetStockAreaList(StockID);
        this.ddlArea.DataBind();
    }

    protected void DataBandSearchStockList()
    {
        this.ddlStockSearch.Items.Clear();
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        this.ddlStockSearch.DataSource = storeHouseBLL.GetStockList();
        this.ddlStockSearch.DataBind();
        this.ddlStockSearch.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void DataBandSearchAreaList(string StockID)
    {
        this.ddlAreaSearch.Items.Clear();
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        if (StockID != "-1")
        {
            this.ddlAreaSearch.DataSource = storeHouseBLL.GetStockAreaList(StockID);
        }
        this.ddlAreaSearch.DataBind();
        this.ddlAreaSearch.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void DataBand()
    {
        FloorBLL floorBLL = new FloorBLL();
        SearchResult.DataSource = floorBLL.GetFloorList().ToList();
        SearchResult.DataBind();
    }

    protected void updateButton_Click(object sender, EventArgs e)
    {
        FloorBLL floorBLL = new FloorBLL();
        if (floorBLL.IsExisted(this.ddlArea.SelectedValue, this.txtFloorName.Text.Trim()))
        {
            Jscript.AjaxAlert(this, "层名称已存在，请重新输入！");
            return;
        }
        
        if (action == "edit")
        {
            string ID = Request.QueryString["ID"].ToString();

            T_FLOOR floor = floorBLL.GetFloorByID(ID);
            floor.AREA_ID = new Guid(this.ddlArea.SelectedValue);
            floor.FloorName = this.txtFloorName.Text.Trim() ;
            floorBLL.UpdateFloor(floor);

            action = null;
            updateButton.Text = "添加";
            this.txtFloorName.Text = "";
            this.ddlStock.SelectedIndex = 0;
            this.ddlArea.SelectedIndex = 0;
        }
        else
        {
            T_FLOOR floor = new T_FLOOR();
            floor.AREA_ID = new Guid(this.ddlArea.SelectedValue);
            floor.FloorName = this.txtFloorName.Text.Trim();
            floorBLL.AddFloor(floor);

        }

        DataBand();
    }
    protected void SearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchResult.PageIndex = e.NewPageIndex;
        action = null;
        this.txtFloorName.Text = "";
        init();
    }
    protected void ddlStock_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBandAreaList(this.ddlStock.SelectedValue);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SearchDataBand();
    }

    protected void SearchDataBand()
    {
        FloorBLL floorBLL = new FloorBLL();

        //if (this.ddlStockSearch.SelectedValue != "-1")
        //{ 
        //    floorBLL.AddGuidEqualCondition(""
        //}
        //if (this.ddlAreaSearch.SelectedValue != "-1")
        //{
        //    floorBLL.AddGuidEqualCondition("AREA_ID", this.ddlAreaSearch.SelectedValue);
        //}
        if (this.txtFloorNameSearch.Text.Trim() != "")
        {
            floorBLL.AddLikeCondition("FloorName", this.txtFloorNameSearch.Text.Trim());
        }

        var search = floorBLL.GetFloorList();
        if (this.ddlStockSearch.SelectedValue != "-1")
        {
            search = search.Where(t => t.T_STOCK_AREA.STOCK_ID.ToString() == this.ddlStockSearch.SelectedValue);
        }
        if (this.ddlAreaSearch.SelectedValue != "-1")
        {
            search = search.Where(t => t.AREA_ID.ToString() == this.ddlAreaSearch.SelectedValue);
        }
        SearchResult.DataSource = search.ToList();
        SearchResult.DataBind();
    }
    protected void ddlStockSearch_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBandSearchAreaList(this.ddlStockSearch.SelectedValue);
    }
}
